using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Ras2ClassLib;

namespace Ras2
{
	public enum ListColumns
	{
		colName = 0,
		colBegin = 1,
		colEnd = 2,
		NumberOfColumns = 3
	}

	public partial class ListView : UserControl
	{
		#region Member variables
		private SortOrder[] m_arrLastSortOrder = new SortOrder[(int)ListColumns.NumberOfColumns];
		private VinListDoc m_document;
		#endregion

		#region Properties
		public VinListDoc Document
		{
			get { return m_document; }
			set { m_document = value; }
		}
		#endregion

		public ListView()
		{
			InitializeComponent();
		}

		private void OnSortList(object sender, ColumnClickEventArgs e)
		{
			SortOrder lastOrder = m_arrLastSortOrder[e.Column];
			m_arrLastSortOrder[e.Column] = (lastOrder == SortOrder.Ascending) ? SortOrder.Descending : SortOrder.Ascending;
			m_listViewList.ListViewItemSorter = new ListSorter((ListColumns)e.Column, lastOrder);
		}

		private void OnDoubleClickList(object sender, EventArgs e)
		{
			ViewList();
		}

		public void ViewList()
		{
			if (m_listViewList.SelectedItems.Count == 1)
			{
				ListViewItem listViewItem = m_listViewList.SelectedItems[0];
				VinList list = (VinList)listViewItem.Tag;

				using (ListDlg dlg = new ListDlg())
				{
					dlg.VinList = list;
					dlg.ShowDialog();
				}
			}
		}

		private ListViewItem GetListViewItem(VinList list)
		{
			ListViewItem item = new ListViewItem(list.Name);

			item.SubItems.Add(Convert.ToString(list.Begin));

			item.SubItems.Add(Convert.ToString(list.End));

			item.ImageIndex = 0;

			item.Tag = list;

			return item;
		}


		protected void HandleError(Exception ex)
		{
			MessageBox.Show("Eftirfarandi villa kom upp: \n\n" + ex.Message);
		}

		private void OnLoad(object sender, EventArgs e)
		{
				
			if (!this.DesignMode)
			{
				m_listViewList.Items.Clear();

				VinListDoc doc = new VinListDoc();
				VinListCollection lists = doc.GetAllVinList();

				foreach (VinList list in lists)
				{
					m_listViewList.Items.Add(GetListViewItem(list));
				}
			}
		
		}

		private void OnMenuViewList(object sender, EventArgs e)
		{
			ViewList();
		}
	}
}
	
